/** @file parser.h 
 *  @brief Parser
 */
#ifndef PARSER_H
#define PARSER_H

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

struct runTime
{
    long long int palindromes;
    long long int primes;
};


/**
* @brief Executes the program in the number of hosts defined for words and phrases
* @param argc the same as main
* @param argv the same as main
* @return void
*/
void distributed_phrase(int argc, char *argv[]);


/**
* @brief Executes the program in the number of hosts defined for words
* @param argc the same as main
* @param argv the same as main
* @return void
*/
void distributed_word(int argc, char* argv[]);

/**
* @brief Break the text into valid words, discarding anything else
* @param buffer The buffer containing the words
* @begin Where to start
* @param length Where to stop
* @param runTime Get the run time
* @return The number of words
*/
static int parser(char *buffer, int begin, int length, struct runTime *);

/**
* @brief Read a file and fill the buffer
* @param length The variable to store the length
* @param fileName File path
* @return Returns the buffer filled
*/
char * fillBuffer(int *length,char *fileName);

/**
* @brief Checks if the word is a palindrome
* @param buffer The buffer that contains the word
* @param begin Position of the first letter
* @param end Position of the last letter
* @return Return 1 if is a palindrome, 0 otherwise
*/
int processWord(char * buffer, int begin, int end);

/**
* @brief Given a character check if it is a letter
* @param ch The character to be checked
* @return Return 1 if is a letter, 0 otherwise
*/
static int isLetter(char ch);

/**
* @brief Given a character check if it is a number
* @param ch The character to be checked
* @return Return 1 if is a number, 0 otherwise
*/
static int isNumber(char);

/**
* @brief Given a character returns it capital
* @param ch The character
* @return The capital character
*/
static char toUpperCase(char ch);


/**
* @brief Given a character check if is an alpha numeric char
* @param ch The character
* @return Return 1 if is alphanumeric, 0 otherwise
*/
static int isAlpha(char);

/**
* @brief Break the text into valid phrases, discarding anything else
* @param buffer The buffer containing the phrases
* @param begin Position to start
* @param length Length to be analyzed
* @param timeElapsed Time to process the text and find the palindromes
* @return 1 in case of any error, 0 otherwise
*/
int parserPhrase(char *buffer, int begin, int length, long long int *timeElapsed);

/**
* @brief Checks if the sentence is a palindrome
* @param buffer The buffer that contains the sentence
* @param begin Position of the first letter
* @param end Position of the last letter
* @return Return 1 if is a palindrome, 0 otherwise
*/
int processPhrase(char *buffer, int begin, int end);

/**
* @brief Given a character determine if it is a termination characater
* @param ch The char
* @return 1 if positive, 0 otherwise
*/
int isTermCh(char ch);


/**
* @brief Sum ASCII values given a word
* @param ch The buffer
* @param begin Beginning of the word
* @param end Last letter of the word
* @return The sum
*/
int sumCh(char *, int begin, int end);


/**
* @brief Calculate the time difference
* @param result Where to store the calculated value
* @param t2 End time
* @param t1 Begin time
*/
void time_sub(struct timeval *result, struct timeval *t2, struct timeval *t1);


/**
* @brief Transform time value from timeval to Long Long Int
* @param The value to be transformed
* @return The value in long long int
*/
long long int tolld(struct timeval *);

#endif
